<?php
/*
 GALLERY.INC.PHP by martin $ 2009/04/14 00:19:07
*/

if(!defined('ENTRY_GALLERY')){
 define('ENTRY_GALLERY', 16); #画像ギャラリーでの1ページあたりのサムネイル数(偶数を指定)
}

$MRSS = is_file('feeds/gallery1.rss') ? TRUE : FALSE;
$link = MODE_STATIC ? ROOT_PATH.'gallery/' : ROOT_PATH.'index.php?mode=gallery';
$cat_select = cat_select($aim, 'all|すべて', ' onchange="location=\''.ROOT_PATH.(MODE_STATIC ? 'gallery/' : 'index.php?mode=gallery&amp;aim=').'\'+(this.value)+\''.$apres.'\'"', 'aim', TRUE);

$db=DB.'images_linked.db';

if(is_file($db)){
 $images = file($db);
} else return;

$_images = array();

foreach ($images as $img){
 if(substr($img, 10, 1)=='d' || strpos($img, '.png') || strpos($img, '.gif') || strpos($img, '.bmp')) continue;
 $_images[] = $img;
}
$images = $_images;

if($aim != '' && $aim != 'all'){ # Category Sort
 $_images = array();
 $target = preg_replace('{^(.+?)\|.+}','$1', trans_cat($aim, FALSE));
 $target = SEE_CAT_HIERARCHY ? '|'.$target.'|' : $target;
 foreach ($images as $i=>$img){
  $parts = explode("\t", $img);
  if(strpos($parts[0], $target)) $_images[] = $img;
 }
 $images = $_images;
}

$total = count($images);

$pitch = ENTRY_GALLERY;
$offset = $pitch * ($page-1);

$pages = ceil($total/$pitch);
$pageBlock = ceil($page / 10);

$images = array_slice($images, $offset, $pitch);
$pagelink = MODE_STATIC ? ($aim != '' ? $aim.'/' : '').'page/' : ($aim != '' ? '&amp;aim='.$aim.'&amp;' : '&amp;').'page=';

# Page Navigation
$pageNav = '<table class="page-nav-main"><tr>';
if($page > 1){
 $pageNav .= '<td class="avant">&nbsp;</td><td class="centre"><a title="前へ" href="'.$link.$pagelink.($page-1).$apres.'">Prev</a></td><td class="apres">&nbsp;</td>';
}
for ($i=($pageBlock-1)*10+1;$i<=$pages;$i++){
 if($i==$page){
  $pageNav .= '<td class="current" style="font-weight:800;">'.$i.'</td>';
 } else {
  $pageNav .= '<td class="link"><a href="'.$link.$pagelink.$i.$apres.'">'.$i.'</a></td>';
 }
 if($i%10==0) break;
}
if($page < $pages){
 $pageNav .= '<td class="avant">&nbsp;</td><td class="centre"><a href="'.$link.$pagelink.($page+1).$apres.'">Next</a></td><td class="apres">&nbsp;</td>';
}
if($pages > 10 && ($pages-$pageBlock*10) > 0 ){
 $pageNav .= '<td class="avant">&nbsp;</td><td class="centre"><a href="'.$link.$pagelink.($pageBlock*10+1).$apres.'">
 '.($pB=$pageBlock*10+1).'-'.($pages > $pB+9 ? $pB+9 : $pB+$pages%10-1).' of '
 .($pages).' pages</a></td><td class="apres">&nbsp;</td>';
}
$pageNav .= '</tr></table>'.NL;

$nav2 = '<table class="page-nav-sub"><tr>
<td class="avant">&nbsp;</td><td class="centre" style="font-weight:500;">カテゴリー別 &raquo;</td><td class="apres">&nbsp;</td>
<td class="avant">&nbsp;</td><td class="centre">'.$cat_select.'</td><td class="apres">&nbsp;</td>
<td class="avant">&nbsp;</td><td class="centre">'.$total.' Entries</td><td class="apres">&nbsp;</td>
</tr></table>';

$DIVISION['header'] .= ($total<=$pitch?'':$pageNav).NL.$nav2.NL;

if($total==0){
 $body = '<p class="center alert" style="margin-top:60px;">このカテゴリーで使われている画像はありません</p>';
} else {
 if($MRSS){
  $DIVISION['header'] .= '<div class="cooliris-link">
  <a href="javascript:PicLensLite.start({feedUrl:\''.ROOT_PATH.'feeds/gallery1.rss'.'\'});">Slide show powered by Cooliris
  <img title="Launch SlideShow" alt="" src="'.(is_file($theme_dir.'PicLensButton.png') ? $theme_dir.'PicLensButton.png' : 'http://lite.piclens.com/images/PicLensButton.png').'" /></a>
 </div>';
 }
 $body = '<div class="gallery"><ul>'.NL;
 $count = count($images);
 foreach ($images as $i=>$info){
  $split = explode("|", $info); 
  $len = sizeof($split);// IMG: 9, 12
  $id = preg_replace('{^0+}', '', $split[0]); $mdate = $split[1]; $name = $split[2]; $w = trim($split[3]); $h = $split[4]; $fs = $split[5];
  $title = $split[7];
  if($len==12){
   $tw = trim($split[8]); $th = $split[9]; $tfs = $split[10];
  }
  if(strpos($title, ',')){
   list($title, $pm_title) = explode(',', $title);
   $pm_link = date('Ym-', $id).$pm_title.EXT_STATIC;
  } else $pm_link = MODE_STATIC ? $id.EXT_STATIC : $_self.'?UID='.$id;
  if($len==9){ // No thumbnail
   $body .= '<li><a href="'.$pm_link.'"><img src="Images/cell10.png" style="background:#fff url('.IMG_DIR.$id.'_'.$name.') no-repeat;" width="'.THUMB_ISIZE.'" height="'.THUMB_ISIZE.'" alt="" title="ファイル名 :'.$name.'&#13;&#10;　　サイズ :'.$fs.' KB&#13;&#10;　　大きさ :'.$w.' x '.$h.'&#13;&#10;エントリー : '.$title.'" /><span class="entry">'.date('Y-m-d', $id).'</span></a></li>'.NL;
  } else {     // Thumbnail exists
   $body .= '<li><a href="'.$pm_link.'"><img src="Images/cell10.png" style="background:#fff url('.IMG_DIR.THUMB1.$id.'_'.$name.') no-repeat 50% 50%;" width="'.THUMB_ISIZE.'" height="'.THUMB_ISIZE.'" alt="" title="ファイル名 : '.$name.'&#13;&#10;　　サイズ : '.$fs.' KB&#13;&#10;　　大きさ : '.$w.' x '.$h.'&#13;&#10; エントリー : '.$title.'" /><span class="entry">'.date('Y-m-d', $id).'</span></a></li>'.NL;
  }
 }
 $body .= '</ul></div>
 <hr class="clear" />
 ';
}

if($MRSS) $DIVISION['script'] .= ' <script type="text/javascript" src="http://lite.piclens.com/current/piclens.js"></script>';

$DIVISION['body'] .= $body;
?>
